home *** CD-ROM | disk | FTP | other *** search
- Path: soap.news.pipex.net!pipex!usenet
- From: tone@dial.pipex.com (Tone)
- Newsgroups: comp.lang.c
- Subject: String assignments, please help (beginner)
- Date: Mon, 08 Apr 1996 15:39:36 GMT
- Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX)
- Message-ID: <4ke05g$27q@soap.news.pipex.net>
- NNTP-Posting-Host: an040.du.pipex.com
- X-Newsreader: Forte Free Agent v0.55
-
- I've just been learning C for the last few weeks and have come across
- a problem with assigning strings, which I will detail below.
- (Sorry the posting is so long, I'm just trying to make sure you know
- exactly what my problem is):
-
- I was originally informed that the following was valid:
-
- char string1[30];
- string1 = "Test string";
-
- This gave me an error on the second line although when declaring I am
- able to use:
-
- char string1[30] = "Test string";
-
- I have then read up and found the following info elsewhere (quote):
-
- a = "This is a string.";
- is only possible if a is a char pointer.
-
- so I arrived at the following:
-
- char *string1[30];
- *string1 = "Test string";
-
- This compiles okay and gives the desired result in the small program I
- was writing.
-
- However, I have since found the following information (quote):
-
- int *c;
- *c = 4;
-
- can and probably will give some unexpected and unwelcome results!
- As the value of c is random, the memory-address to which 4 is
- assigned is random, too. This means that you could change basically
- any location in the memory, including machine code.
-
- I am assuming that these "unexpected results" mentioned above will
- also apply to the code I have written.
-
- The only other way I know of assigning a string is to create a loop
- (e.g. a "for" loop) and assign each character of the string
- individually and manually add the '\0' at the end. This seems
- extremely laborious and I'm sure it can't be the best way. (Also it
- may give similar "unexpected results"?)
-
- My questions are as follows:
-
- 1) When using pointers as shown above, do I need to set the memory
- location of the string variable in some way before using it (and if
- so, how).
- 2) Am I going about string assignments the right way. If not, could
- somebody please show me the most simple way.
-
- Any help in the above would be greatly appreciated
- Tone
-
- tone@dial.pipex.com
-
-